<?php
if(!defined('IN_SITE')) {
	exit('Access Denied');
}

class server_node{
	public $info_array = array();
	public $page_array = array();
	public $tree_array = array();

	static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}
	/*
	*	取出一条信息
	*/
	public function one_info($sn_id='') {
		$info = array();
		if($sn_id){
			$sql = "SELECT * FROM ".DB::table('server_node')." WHERE sn_id='".$sn_id."' LIMIT 1";
			$info = DB::fetch_first($sql);	
		}
		return $info;
	}
    
	/*
	*  添加系统信息
	*/
	public function add() {
        global $_G;
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$server_node = isset($_REQUEST['server_node']) && !empty($_REQUEST['server_node']) ? $_REQUEST['server_node']:'';
		if('1'==$issubmit){
            //TODO 后端字段校验
            $server_node['create_dateline'] = TIMESTAMP;
            $insert_id = DB::insert('server_node',$server_node,true);
            if($insert_id){
                showmessage('操作成功','index.php?mod=server&action=node&do=index');
            }else{
                showmessage('操作失败或信息未发生变更','index.php?mod=server&action=node&do=index');
            }
		}else{
			include template('server/node/add');
		}
	}
    
	/*
	*  编辑系统信息
	*/
	public function edit() {
        global $_G;
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$server_node = isset($_REQUEST['server_node']) && !empty($_REQUEST['server_node']) ? $_REQUEST['server_node']:'';
        $sn_id = isset($_REQUEST['sn_id']) ? $_REQUEST['sn_id'] : '';
        if($sn_id){
            if('1'==$issubmit){
                $server_node['modify_dateline'] = TIMESTAMP;
                $where = array('sn_id'=>$sn_id);
                $effect_row = DB::update('server_node',$server_node,$where);
                if($effect_row){
                    showmessage('操作成功','index.php?mod=server&action=node&do=index');
                }else{
                    showmessage('操作失败','index.php?mod=server&action=node&do=index');
                }
            }else{
                //DEBUG 获取节点信息
                $info = self::one_info($sn_id);
                include template('server/node/edit');
            }
        }else{
            showmessage('请选择编辑对象','index.php?mod=server&action=node&do=index');
        }
	}
    
	/*
	*  删除系统信息
	*/
	public function delete() {
        $sn_id = isset($_REQUEST['sn_id']) ? $_REQUEST['sn_id'] : '';
        if($sn_id){
            DB::delete("server_node", array("sn_id" => $sn_id), $limit=1);
            //TODO 逻辑删需要创建isdelete字段 0=未删除 1=已删除
            //DB::update("server_node",array('isdelete'=>1), array("sn_id" => $sn_id), $limit=1);
            showmessage('操作成功','index.php?mod=server&action=node&do=index');
        }
    }
	/*
	*  系统信息列表
	*/
	public function index(){
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start=(($page-1) * $perpage);
        $wheresql = "";
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        if($keyword){
            $wheresql = " AND sn_ipv4_intranet LIKE '%".$keyword."%'";
        }
        $sql_info = "SELECT * FROM ".DB::table('server_node')." WHERE 1=1 ".$wheresql." ORDER BY sn_id DESC ".DB::limit($start, $limit);
        $sql_info_result = DB::fetch_all($sql_info);
        $sql_total_rows = "SELECT count(*) FROM ".DB::table('server_node')." WHERE 1=1 ".$wheresql."";
        $sql_total_rows_result = DB::result_first($sql_total_rows);
        $multipage = multi($sql_total_rows_result, $perpage, $page, "index.php?mod=server&action=node&do=index");
		include template('server/node/index');	
	}

   /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
	public function api(){
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start=(($page-1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if($keyword){
            $wheresql = " WHERE title LIKE '%".$keyword."%'";
        }
        $sql_info = "SELECT sn_id FROM ".DB::table('server_node')." WHERE 1=1 ".$wheresql." ORDER BY sn_id DESC ".DB::limit($start, $limit);
        $sql_info_result = DB::fetch_all($sql_info);
        if('json'==$return_type){
            $return = json_encode($sql_info_result);	
        }elseif('xml'==$return_type){
            $return = array2xml($sql_info_result);
        }
		echo $return;
	}
    
	/*
	* ajax 返回信息
	*/
	public function return_json($error,$message,$output=1) {
		$return_array = array(
			"error" => $error,
			"message" => $message
		);
		if($output){
			echo json_ext($return_array);
			die;
		}else{
			return json_ext($return_array);
		}
	}
}
?>